package cn.yoyo.service;

import cn.yoyo.entity.BookType;
import cn.yoyo.mapper.BookTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Service
@Configuration
public class BookTypeServiceImpl implements BookTypeService {
    @Autowired(required = false)
    BookTypeMapper bookTypeMapper;

    @Override
    public int add(String type, Integer parentid, Integer priority) {
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("parentid", parentid);
        map.put("priority", priority);
        return bookTypeMapper.add(map);
    }

    @Override
    public int update(Integer id, String type, Integer priority) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("type", type);
        map.put("priority", priority);
        return bookTypeMapper.update(map);
    }

    @Override
    @Transactional
    public List<Map<String, Object>> findAll() {
        // 将数据包装成前端 el-table 的tree树形结构所需要的数据结构返回出去
        List<BookType> lst1 = bookTypeMapper.findAll(); // 所有的父类型
        List<BookType> lst2 = bookTypeMapper.findAllCType();// 所有的子类型
        List<Map<String, Object>> allList = new ArrayList<>();
        for (int i = 0; i < lst1.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", lst1.get(i).getId());
            map.put("type", lst1.get(i).getType());
            map.put("parentid", lst1.get(i).getParentid());
            map.put("priority", lst1.get(i).getPriority());
            map.put("isdel", lst1.get(i).getIsdel());
            map.put("children", new ArrayList<>());
            allList.add(map);
            for (int j = 0; j < lst2.size(); j++) {
                if (lst2.get(j).getParentid() == lst1.get(i).getId()) {
                    List list = (List) allList.get(i).get("children");// 获取匹配的children数组
                    list.add(lst2.get(j));// 把该类型的子类型加到其children数组里
                }
            }
        }
        return allList;
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        BookType type = bookTypeMapper.findById(id);
        System.out.println(type);
        // 如果禁用的是父类型的话,则该类型下的所有子类型也被禁用
        if (type.getParentid() == 0) {
            bookTypeMapper.deleteCType(type.getId());
        }
        return bookTypeMapper.delete(id);
    }

    @Override
    @Transactional
    public int unlock(Integer id) {
        BookType type = bookTypeMapper.findById(id);
        // 如果解锁的是父类型的话,则该类型下的所有子类型也被解锁
        if (type.getParentid() == 0) {
            bookTypeMapper.unlockCType(type.getId());
        }
        return bookTypeMapper.unlock(id);
    }

    @Override
    public BookType findById(Integer id) {
        return bookTypeMapper.findById(id);
    }
}
